package com.attendance.www.hr.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.attendance.www.hr.beans.DictionaryBean;
import com.attendance.www.hr.beans.DictionaryPageBean;
import com.attendance.www.hr.beans.UserEmployeeBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.entitys.Dictionary;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IDictionaryService;
import com.zhangkongli.frame.core.ResultMap;
/**
 * 字典表
 * @version 2017/9/28-2017/9/29
 * @author 徐伟
 *
 */
@Controller
@RequestMapping("/common")
public class DictionaryController {

	@Resource
	private IDictionaryService dictionaryService;

	/**
	 * 获取部分字典数据接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDictionaryListBydicCls.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDictionaryListBydicCls(HttpServletRequest request) {
		String dicCls = request.getParameter("dicCls");
		String dicCd = request.getParameter("dicCd");
		UserEmployeeBean userEmployee = (UserEmployeeBean)request.getSession().getAttribute(AppConstant.SESSIONUSER);
		if(userEmployee == null){
			throw new ResultFaultException("1_00001");
		}
		Integer employeeId = userEmployee.getEmployeeId();
		if (StringUtils.isBlank(dicCls)) {
			throw new ResultFaultException("0_00001");
		}
		List<DictionaryBean> dictionaryBeanList = dictionaryService.getDictionaryListByDicCls(dicCls,dicCd,employeeId);
		return ResultMap.success(dictionaryBeanList);
	}
	
	/**
	 * 获取部分字典数据接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDictionaryBydicCls.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDictionaryBydicCls(HttpServletRequest request) {
		String dicCls = request.getParameter("dicCls");
		if (StringUtils.isBlank(dicCls)) {
			throw new ResultFaultException("0_00001");
		}
		List<DictionaryBean> dictionaryBeanList = dictionaryService.getDictionaryByDicCls(dicCls);
		return ResultMap.success(dictionaryBeanList);
	}

	/**
	 * 获取全部字典数据接口
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getDictionaryList.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object getDictionaryList(HttpServletRequest request) {
		String pageNoString = request.getParameter("pageNum");
		String pageCountString = request.getParameter("rowCount");
		Integer pageNo = Integer.parseInt(pageNoString);
		Integer pageCount = Integer.parseInt(pageCountString);
		Integer startNum = 0;
		if (pageNo > 1) {
			startNum = (pageNo - 1) * pageCount;
		}
		DictionaryPageBean dictionaryPageBean = new DictionaryPageBean();
		dictionaryPageBean = dictionaryService.getDictionaryList(startNum, pageCount);
		return ResultMap.success(dictionaryPageBean);
	}

	/**
	 * 保存一条字典数据接口
	 * @param request
	 * @param dictionary
	 * @return
	 */
	@RequestMapping(value = "/saveDictionary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object saveDictionaryListBydicCls(HttpServletRequest request, @RequestBody Dictionary dictionary) {
		if (StringUtils.isBlank(dictionary.getDicCd()) || StringUtils.isBlank(dictionary.getDicCls())
				|| StringUtils.isBlank(dictionary.getDicClsName()) || StringUtils.isBlank(dictionary.getDicName())) {
			throw new ResultFaultException("0_00001");
		}
		if (dictionaryService.getDictionaryListByDicClsORDicCd(dictionary.getDicCls(), dictionary.getDicCd()) == null) {
			if (dictionaryService.saveDictionary(dictionary)) {
				return ResultMap.success();
			}
		} else {
			throw new ResultFaultException("0_00010");
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 修改字典数据接口
	 * @param request
	 * @param dictionary
	 * @return
	 */
	@RequestMapping(value = "/updateDictionary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object updateDictionaryListBydicCls(HttpServletRequest request, @RequestBody Dictionary dictionary) {
		if (StringUtils.isBlank(dictionary.getDicCd()) || StringUtils.isBlank(dictionary.getDicCls())
				|| StringUtils.isBlank(dictionary.getDicClsName()) || StringUtils.isBlank(dictionary.getDicName())) {
			throw new ResultFaultException("0_00001");
		}
		if (dictionaryService.getDictionaryListByDicClsORDicCd(dictionary.getDicCls(), dictionary.getDicCd()) != null) {
			if (dictionaryService.updateDictionary(dictionary)) {
				return ResultMap.success();
			}
		} else {
			throw new ResultFaultException("0_00012");
		}
		throw new ResultFaultException("0_00001");
	}

	/**
	 * 删除字典数据接口
	 * @param request
	 * @param dictionary
	 * @return
	 */
	@RequestMapping(value = "/deleteDictionary.do", method = { RequestMethod.POST, RequestMethod.GET })
	@ResponseBody
	public Object deleteDictionaryListBydicCls(HttpServletRequest request, @RequestBody Dictionary dictionary) {
		if (StringUtils.isBlank(dictionary.getDicCd()) || StringUtils.isBlank(dictionary.getDicCls())) {
			throw new ResultFaultException("0_00001");
		}
		if (dictionaryService.deleteDictionary(dictionary)) {
			return ResultMap.success();
		}
		throw new ResultFaultException("0_00001");
	}

}
